package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.app.dao.AgentDao;
import io.renren.modules.app.entity.AgentEntity;
import io.renren.modules.app.service.AgentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther zxl
 * @Date 2018/12/16 9:43
 * @Description
 **/
@Service("AgentService")
public class AgentServiceImpl extends ServiceImpl<AgentDao, AgentEntity> implements AgentService {

    @Override
    public boolean addOneAgent(AgentEntity agentEntity) {
        agentEntity.setCreatTime(new Date());
        boolean b = this.insert(agentEntity);

        return b;
    }

    @Override
    public AgentEntity queryById(Integer agentId) {
        AgentEntity agentEntity = new AgentEntity();
        agentEntity.setAgentId(agentId);
        return baseMapper.selectOne(agentEntity);
    }

    @Override
    public AgentEntity queryByName(String agentName) {

        AgentEntity agentEntity = new AgentEntity();
        agentEntity.setAgentName(agentName);

        return baseMapper.selectOne(agentEntity);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String agentName = (String) params.get("agentName");

        Page<AgentEntity> page = this.selectPage(
                new Query<AgentEntity>(params).getPage(),
                new EntityWrapper<AgentEntity>()
                .like(StringUtils.isNotBlank(agentName),"agent_name",agentName)
        );
        return new PageUtils(page);
    }

    @Override
    public List<AgentEntity> selectAll(String agentName,String agent_province, String city) {

        return baseMapper.selectList(new EntityWrapper<AgentEntity>()
        .like(StringUtils.isNotBlank(agentName),"agent_name",agentName)
        .like(StringUtils.isNotBlank(agentName),"agent_province",agent_province)
        .like(StringUtils.isNotBlank(city),"agent_city",city));

    }

    @Override
    public boolean update(AgentEntity agentEntity) {
        boolean b = this.updateById(agentEntity);
        return b;
    }
}
